package com.dashen.general.server.controller;

import org.apache.logging.log4j.util.Strings;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.WebAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @auther barry
 * @date 2019/1/14
 */
@Controller
public class LoginController {

    @RequestMapping("initLoginPage")
    public String selfLogin(HttpServletRequest request, HttpServletResponse response
            , @RequestParam(value = "err", required=false) String err, Model model){
//        if(!Strings.isBlank(err)){
//            model.addAttribute("err", err);
//        }

        HttpSession session = request.getSession(false);

        if (session != null) {
            AuthenticationException ex = (AuthenticationException) session
                    .getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
            model.addAttribute("err" ,ex != null ? ex.getMessage() : "none");
        }
        return "customLogin" ;
    }

}
